<script lang="ts" setup>
import { ref, onMounted } from "vue"
import { updateCompanyApi, getCompanyApi } from "@/api/company"
import { type UpdateCompanyRequestData } from "@/api/company/types"
import { getImgApi } from "@/api/common"
import { type FormInstance, type FormRules, ElMessage, UploadUserFile } from "element-plus"
import { CODE_SUCCESS } from "@/utils/service"
import { computed } from "vue"
import IEditor from "@/components/IEditor/index.vue"

defineOptions({
  // 命名当前组件
  name: "Company"
})
const loading = ref<boolean>(false)

//#region 增
const DEFAULT_FORM_DATA: UpdateCompanyRequestData = {
  name: "",
  weixin: "",
  phone: "",
  qq: "",
  sina_weibo: "",
  logo_url: "",
  footprint: 1,
  production_workshop: 1,
  laboratory: 1,
  writing_ink: 1,
  inkjet_printing: 1,
  introduction: "",
  address: ""
}
const dialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const formData = ref<UpdateCompanyRequestData>(JSON.parse(JSON.stringify(DEFAULT_FORM_DATA)))

const formRules: FormRules<UpdateCompanyRequestData> = {
  name: [{ required: true, trigger: "blur", message: "请输入公司名称" }],
  weixin: [{ required: true, trigger: "blur", message: "请填写微信号" }],
  // phone: [{ required: true, trigger: "blur", message: "请填写手机号" }],
  qq: [{ required: true, trigger: "blur", message: "请填写qq号" }],
  sina_weibo: [{ required: true, trigger: "blur", message: "请填写微博号" }],
  logo_url: [{ required: true, trigger: "blur", message: "请上传logo图片" }],
  footprint: [{ required: true, trigger: "blur", message: "请填写公司占地(单位：平方米)" }],
  production_workshop: [{ required: true, trigger: "blur", message: "请填写生产车间(单位：平方米)" }],
  laboratory: [{ required: true, trigger: "blur", message: "请填写实验室(单位：平方米)" }],
  writing_ink: [{ required: true, trigger: "blur", message: "请填写书写墨水(单位：吨)" }],
  inkjet_printing: [{ required: true, trigger: "blur", message: "请填写喷印墨水(单位：吨)" }],
  introduction: [{ required: true, trigger: "blur", message: "请填写简介" }],
  address: [{ required: true, trigger: "blur", message: "请填写地址" }]
}
const handleCreateOrUpdate = () => {
  formRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return console.error("表单校验不通过", fields)
    loading.value = true
    updateCompanyApi(formData.value)
      .then(() => {
        ElMessage.success("操作成功")
      })
      .finally(() => {
        loading.value = false
      })
  })
}

//#region 查
const getNewsDetail = () => {
  loading.value = true
  getCompanyApi()
    .then(({ data }) => {
      formData.value = data || DEFAULT_FORM_DATA
    })
    .catch(() => {
      formData.value = DEFAULT_FORM_DATA
    })
    .finally(() => {
      loading.value = false
    })
}
onMounted(() => {
  getNewsDetail()
})
const dialogImgVisible = ref<boolean>(false)
const file = ref<any>()
const handlePreview = async () => {
  const { data } = await getImgApi({ path: formData.value.logo_url })
  if (data) {
    file.value = data
  }
  dialogImgVisible.value = true
}
const handleRemove = () => {
  formData.value.logo_url = ""
}
const handleBeforeUpload = (file: any) => {
  return new Promise((resolve, reject) => {
    const isJPG = file.type === "image/jpeg" || "image/png"
    const isLt2M = file.size / 1024 / 1024 < 2
    if (!isJPG) {
      ElMessage.error("上传图片只能是 JPG、JPEG、PNG 格式!")
      reject()
    }
    if (!isLt2M) {
      ElMessage.error("上传图片大小不能超过 2MB!")
      reject()
    }
  })
}
const handleSuccess = (response: any) => {
  if (response.code === CODE_SUCCESS) {
    formData.value.logo_url = response.data
  }
}
const fileList = computed<UploadUserFile[]>(() => {
  let list: UploadUserFile[] = []
  if (formData.value.logo_url) {
    list = [
      {
        name: formData.value.logo_url.split("/").at(-1) || "",
        url: formData.value.logo_url
      }
    ]
  }
  return list
})
</script>

<template>
  <div class="app-container">
    <!-- 新增/修改 -->
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>公司信息</h3>
        </div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="200px" label-position="left">
        <el-form-item prop="name" label="名称">
          <el-input v-model="formData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="name_en" label="英文名称">
          <el-input v-model="formData.name_en" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="weixin" label="微信号">
          <el-input v-model="formData.weixin" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="phone" label="手机号">
          <el-input v-model="formData.phone" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="qq" label="QQ号">
          <el-input v-model="formData.qq" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="sina_weibo" label="新浪微博号">
          <el-input v-model="formData.sina_weibo" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="logo_url" label="公司logo">
          <el-upload
            :file-list="fileList"
            class="upload-demo"
            action="/api/uploadImg"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :limit="1"
            :before-update="handleBeforeUpload"
            :on-success="handleSuccess"
          >
            <el-button type="primary">上传图片</el-button>
            <template #tip>
              <div class="el-upload__tip">logo图片不能超过 2M.</div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item prop="footprint" label="公司占地(单位：平方米)">
          <el-input-number v-model="formData.footprint" :min="1" :precision="2" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="production_workshop" label="生产车间(单位：平方米)">
          <el-input-number v-model="formData.production_workshop" :min="1" :precision="2" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="laboratory" label="实验室(单位：平方米)">
          <el-input-number v-model="formData.laboratory" :min="1" :precision="2" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="writing_ink" label="书写墨水(单位：吨)">
          <el-input-number v-model="formData.writing_ink" :min="1" :precision="2" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="inkjet_printing" label="喷印墨水(单位：吨)">
          <el-input-number v-model="formData.inkjet_printing" :min="1" :precision="2" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="introduction" label="简介">
          <IEditor v-model="formData.introduction" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="introduction_en" label="英文简介">
          <IEditor v-model="formData.introduction_en" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="address" label="地址">
          <el-input v-model="formData.address" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确认</el-button>
      </template>
    </el-card>
    <el-dialog v-model="dialogImgVisible" title="图片预览" width="800" align-center>
      <img :src="file" style="width: 700px" />
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogImgVisible = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
@/api/news/types
